//
//  userAppInfoStack.swift
//  demo01
//
//  Created by babybus on 2025/7/1.
//

import Foundation
import UIKit
import SnapKit
import Alamofire

class UserAppInfoStack:UIStackView{
    //全局变量
    var MyCloudDrive:UserInfoBlockStack!
    var collect:UserInfoBlockStack!
    var footprint:UserInfoBlockStack!
    var allItems:[UserAppInfo]=[]
    let api:String = "/userappinfo"
    //初始化函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        loadData(userId: 1)
        attributeInit()
        
    }
    required init(coder: NSCoder) {
        fatalError("init(coder:NSCoder) -- fatalError")
    }
    //属性设置
    func attributeInit(){
        axis = .horizontal
        alignment = .fill
        distribution = .fill
        spacing = 8
    }
    //整体布局
    func setupUI(){
        //模块创建
        MyCloudDrive = UserInfoBlockStack()
        collect = UserInfoBlockStack()
        footprint = UserInfoBlockStack()
        //第一个模块内容
        MyCloudDrive.setBlockName(name: allItems[0].blockName)
        MyCloudDrive.configure(numberlable: allItems[0].userInfoBlock[0].numLabel,
                               lablename: allItems[0].userInfoBlock[0].detailLabel)
        MyCloudDrive.configure(numberlable: allItems[0].userInfoBlock[1].numLabel,
                               lablename: allItems[0].userInfoBlock[1].detailLabel)
        
        MyCloudDrive.layer.cornerRadius = 15
        MyCloudDrive.layer.masksToBounds = true
        let color1 = getGradient(c:.cyan)
        MyCloudDrive.backgroundColor = color1
        //第二个模块内容
        collect.setBlockName(name: allItems[1].blockName)
        collect.configure(numberlable: allItems[1].userInfoBlock[0].numLabel,
                          lablename: allItems[1].userInfoBlock[0].detailLabel)
        collect.layer.cornerRadius = 20
        collect.backgroundColor = getGradient(c: UIColor(red: 0.85, green: 0.92, blue: 1.0, alpha: 1.0) )
        //第三个模块内容
        footprint.setBlockName(name: allItems[2].blockName)
        footprint.configure(numberlable: allItems[2].userInfoBlock[0].numLabel,
                            lablename: allItems[2].userInfoBlock[0].detailLabel)
        footprint.layer.cornerRadius = 20
        footprint.backgroundColor = getGradient(c:UIColor(red: 1.0, green: 0.98, blue: 0.8, alpha: 1.0))
        
        //插入背景
        setBackGround(stack: MyCloudDrive, background: UIImageView(image: UIImage(named: "./背景-我的云盘.png")))
        //将模块加入到stack中
        self.addArrangedSubview(MyCloudDrive)
        self.addArrangedSubview(collect)
        self.addArrangedSubview(footprint)
        
        //模块布局
        MyCloudDrive.snp.makeConstraints { make in
            make.top.equalToSuperview().inset(0)
            make.height.equalToSuperview()
            make.width.equalTo(175)
        }
        collect.snp.makeConstraints { make in
            make.top.equalToSuperview().inset(0)
            make.height.equalToSuperview()
            make.width.equalTo(80)
        }
        footprint.snp.makeConstraints { make in
            make.top.equalToSuperview().inset(0)
            make.height.equalToSuperview()
            make.width.equalTo(80)
        }
    }
    func getGradient(c:UIColor)->UIColor {
        let gradientColor = UIColor { traitCollection in
            let colors = traitCollection.userInterfaceStyle == .dark ?
                [UIColor.black, UIColor.purple] :
                [UIColor.white, c]
            
            let gradient = CAGradientLayer()
            gradient.colors = colors.map { $0.cgColor }
            gradient.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
            
            UIGraphicsBeginImageContext(gradient.bounds.size)
            gradient.render(in: UIGraphicsGetCurrentContext()!)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            
            return UIColor(patternImage: image!)
        }
        return gradientColor
    }
    
    func setBackGround(stack:UIStackView ,background:UIImageView){
        background.contentMode = .scaleAspectFit//支持按比例缩放
        background.clipsToBounds = true//不超出界限
        
        stack.insertSubview(background, at: 0)//插入最底部
        background.snp.makeConstraints { make in
            make.edges.equalToSuperview()//铺满父容器
        }
    }
    func loadData(userId:Int){
        let url = AllUrl.LH_URL + api + "?userId=\(userId)"
        AF.request(url,method: .get)
            .validate(statusCode: 200..<300)
            .responseDecodable(of:[UserAppInfo].self){ response in
                switch response.result{
                case .success(let items):
                    DispatchQueue.main.async {
                        self.allItems = items
//                        print(self.allItems)
                        self.setupUI()
                    }
                case .failure(let error):
                    print("错误: \(error.localizedDescription)")
                }
            }
    }
}
